package com.yogee.topseller.fragment.home;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yogee.core.base.HttpFragment;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.topseller.R;
import com.yogee.topseller.adapter.FlashRvAdapter;
import com.yogee.topseller.bean.FastQueryBean;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.utils.AppUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 闪搜fragment
 *
 * @auathor ${XiaoJianghong}
 * @date 2017/7/14.
 */

public class FlashFragment extends HttpFragment {

    @BindView(R.id.flash_sousuo_num_tv)
    TextView flashSousuoNumTv;
    @BindView(R.id.flash_liulan_num_tv)
    TextView flashLiulanNumTv;
    @BindView(R.id.flash_goumai_num_tv)
    TextView flashGoumaiNumTv;
    @BindView(R.id.flash_rv)
    RecyclerView flashRv;
    private FlashRvAdapter adapter;
    private String tel;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_flash;
    }

    @Override
    protected void initView() {
        adapter = new FlashRvAdapter(getActivity());
        flashRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        flashRv.setHasFixedSize(true);
        flashRv.setAdapter(adapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        fastQuery(AppUtil.getUserId(getActivity()));
    }

    @OnClick(R.id.flash_pipei_tv)
    public void onViewClicked() {
        if (!tel.equals("")){
            Intent intent = new Intent(Intent.ACTION_DIAL);
            Uri data = Uri.parse("tel:" + tel);
            intent.setData(data);
            startActivity(intent);
        }
    }

    private void fastQuery(String userId) {

        if(!AppUtil.isExamine(getActivity())){
            HttpManager.getInstance().fastQuery(userId).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<FastQueryBean>() {
                @Override
                public void onNext(FastQueryBean o) {
                    adapter.addData(o.getQueryList(),o.getTel());
                    flashSousuoNumTv.setText(o.getQueryNum());
                    flashLiulanNumTv.setText(o.getBrowseNum());
                    flashGoumaiNumTv.setText(o.getBuyNum());
                    tel = o.getTel();
                }
            }, this));
        }
    }
}
